package cn.tedu.luban.demand.main.controller;

import cn.tedu.luban.demand.main.converter.DemandConverter;
import cn.tedu.luban.demand.main.service.DemandService;
import cn.tedu.luban.demand.protocol.VO.RequestOrderListItemVO;
import cn.tedu.luban.demand.protocol.bo.RequestOrderBO;
import cn.tedu.luban.demand.protocol.query.RequestOrderQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.exception.BusinessException;
import com.tedu.inn.protocol.model.Result;
import com.tedu.inn.protocol.pager.PagerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemandController {


    @Autowired
    private DemandConverter demandConverter;
    @Autowired
    private DemandService demandService;

    @PostMapping("/demand/order/search")
    public Result<PagerResult<RequestOrderListItemVO>> searchRequestOrders(
            @RequestBody RequestOrderQuery query) throws BusinessException {

        // PagerResult 封装5个属性：total, pageSize, pageNo, List, lastPageIndex
        // pageSize, pageNo 就是Query对象中的属性 - RequestOrderQuery
        // total, List数据 业务查询 - ListRecordTotalBO
        // lastPageIndex 根据total, pageSize计算得到 - PagerResult - getLastPageIndex() 总页数
        ListRecordTotalBO<RequestOrderBO> bos = demandService.searchRequestOrders(query);

        // 转换成 PagerResult<RequestOrderListItemVO>
        PagerResult<RequestOrderListItemVO> pagerResult = demandConverter.assemblePagerResult(bos, query);

        return new Result<>(pagerResult);
    }





}












